﻿using CodeNotes.BLL;
using CodeNotes.Model;
using MvcCodeNotes.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcCodeNotes.Controllers
{
    public class JokeController : BaseController
    {
        //
        // GET: /Joke/

        public ActionResult Index()
        {

            return View();
        }

        public ActionResult Detail(int type, int id)
        {
            if (type == 1)
            {
                CW_Joke joke = new CW_JokeLogic().GetModel(id);
                if (joke != null)
                    ViewBag.Joke = joke;
                else
                    return RedirectToAction("Index");
            }
            else if (type == 2)
            {
                CW_JokePicture joke = new CW_JokePictureLogic().GetModel(id);
                if (joke != null)
                    ViewBag.Joke = joke;
                else
                    return RedirectToAction("Index");
            }
            return View();
        }

        //获取首页、图片、文字数据        
        [HttpPost]
        public JsonResult Hybird(int? idx, int? typeid)
        {
            List<object> jokes = new List<object>();
            if (idx == null || idx < 1)
                idx = 1;
            int index = idx <= 1 ? 1 : Convert.ToInt32(idx);
            int oDay = DateTime.Now.Day;
            if (typeid == null)
                typeid = 0;
            int pageSize = 20;
            if (typeid == 0)
                pageSize = 15;

            if (typeid == 0 || typeid == 1)
            {
                SP_Pagination sp = new SP_Pagination
                {
                    TableName = "CW_Joke",
                    Fields = "*",
                    OrderField = "Id",
                    PageSize = pageSize,
                    PageIndex = index,
                    SortAsc = true,
                    strWhere = "IsShow=1"
                };
                DataTable dt = sp.GetList().Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var model = new
                    {
                        Id = dt.Rows[i]["Id"].ToString(),
                        Title = dt.Rows[i]["Title"].ToString(),
                        Content = dt.Rows[i]["Content"].ToString(),
                        LikeCount = dt.Rows[i]["LikeCount"].ToString(),
                        HateCount = dt.Rows[i]["HateCount"].ToString(),
                        Type = 1
                    };
                    jokes.Add(model);
                }
            }

            if (typeid == 0 || typeid == 2)
            {
                SP_Pagination sp2 = new SP_Pagination
                {
                    TableName = "CW_JokePicture",
                    Fields = "*",
                    OrderField = "Id",
                    PageSize = pageSize,
                    PageIndex = index,
                    SortAsc = true,
                    strWhere = "IsShow=1"
                };
                DataTable dt2 = sp2.GetList().Tables[0];
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    var model = new
                    {
                        Id = dt2.Rows[i]["Id"].ToString(),
                        Title = dt2.Rows[i]["Title"].ToString(),
                        Content = dt2.Rows[i]["Content"].ToString(),
                        LikeCount = dt2.Rows[i]["LikeCount"].ToString(),
                        HateCount = dt2.Rows[i]["HateCount"].ToString(),
                        Type = 2
                    };
                    jokes.Add(model);
                }
            }

            if (typeid == 0 || typeid == 3)
            {
                if (typeid == 0)
                    pageSize = 5;
                SP_Pagination sp3 = new SP_Pagination
                {
                    TableName = "CW_JokeVideo",
                    Fields = "*",
                    OrderField = "Id",
                    PageSize = pageSize,
                    PageIndex = index,
                    SortAsc = true,
                    strWhere = "Source!='budejie'"
                };
                DataTable dt3 = sp3.GetList().Tables[0];
                for (int i = 0; i < dt3.Rows.Count; i++)
                {
                    var model = new
                    {
                        Id = dt3.Rows[i]["Id"].ToString(),
                        Title = dt3.Rows[i]["Title"].ToString(),
                        Content = dt3.Rows[i]["Vid"].ToString(),
                        LikeCount = 0,
                        HateCount = 0,
                        Type = 3
                    };
                    jokes.Add(model);
                }
            }

            if (jokes.Count > 0)
            {
                //打乱顺序
                List<object> jokesNew = new List<object>();
                if (typeid == 0)
                    jokesNew = randomList(jokes);
                else
                    jokesNew = jokes;

                var obj = new
                {
                    state = "success",
                    jokes = jokesNew
                };
                return Json(obj);
            }
            else
            {
                var obj = new
                {
                    state = "norecord"
                };
                return Json(obj);
            }

        }

        //小程序获取首页、图片、文字数据        
        [HttpPost]
        public JsonResult WxJoke(int? idx, int? typeid)
        {
            List<object> jokes = new List<object>();
            if (idx == null || idx < 1)
                idx = 1;
            int index = idx <= 1 ? 1 : Convert.ToInt32(idx);
            int oDay = DateTime.Now.Day;
            if (typeid == null)
                typeid = 0;
            int pageSize = 20;
            if (typeid == 0)
                pageSize = 15;

            if (typeid == 0 || typeid == 1)
            {
                SP_Pagination sp = new SP_Pagination
                {
                    TableName = "CW_Joke",
                    Fields = "*",
                    OrderField = "Id",
                    PageSize = pageSize,
                    PageIndex = index,
                    SortAsc = true,
                    strWhere = "IsShow=1"
                };
                DataTable dt = sp.GetList().Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var model = new
                    {
                        Id = dt.Rows[i]["Id"].ToString(),
                        Title = dt.Rows[i]["Title"].ToString(),
                        Content = dt.Rows[i]["Content"].ToString(),
                        LikeCount = dt.Rows[i]["LikeCount"].ToString(),
                        HateCount = dt.Rows[i]["HateCount"].ToString(),
                        Type = 1
                    };
                    jokes.Add(model);
                }
            }

            if (typeid == 0 || typeid == 2)
            {
                SP_Pagination sp2 = new SP_Pagination
                {
                    TableName = "CW_JokePicture",
                    Fields = "*",
                    OrderField = "Id",
                    PageSize = pageSize,
                    PageIndex = index,
                    SortAsc = true,
                    strWhere = "IsShow=1"
                };
                DataTable dt2 = sp2.GetList().Tables[0];
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    var model = new
                    {
                        Id = dt2.Rows[i]["Id"].ToString(),
                        Title = dt2.Rows[i]["Title"].ToString(),
                        Content = dt2.Rows[i]["Content"].ToString(),
                        LikeCount = dt2.Rows[i]["LikeCount"].ToString(),
                        HateCount = dt2.Rows[i]["HateCount"].ToString(),
                        Type = 2
                    };
                    jokes.Add(model);
                }
            }

            if (typeid == 0 || typeid == 3)
            {
                if (typeid == 0)
                    pageSize = 5;
                SP_Pagination sp3 = new SP_Pagination
                {
                    TableName = "CW_JokeVideo",
                    Fields = "*",
                    OrderField = "Id",
                    PageSize = pageSize,
                    PageIndex = index,
                    SortAsc = true,
                    strWhere = "Source='budejie'"
                };
                DataTable dt3 = sp3.GetList().Tables[0];
                for (int i = 0; i < dt3.Rows.Count; i++)
                {
                    var model = new
                    {
                        Id = dt3.Rows[i]["Id"].ToString(),
                        Title = dt3.Rows[i]["Title"].ToString(),
                        Content = dt3.Rows[i]["Pic"].ToString(),
                        LikeCount = 0,
                        HateCount = 0,
                        Type = 3
                    };
                    jokes.Add(model);
                }
            }

            if (jokes.Count > 0)
            {
                //打乱顺序
                List<object> jokesNew = new List<object>();
                if (typeid == 0)
                    jokesNew = randomList(jokes);
                else
                    jokesNew = jokes;

                var obj = new
                {
                    state = "success",
                    jokes = jokesNew
                };
                return Json(obj);
            }
            else
            {
                var obj = new
                {
                    state = "norecord"
                };
                return Json(obj);
            }

        }

        [HttpPost]
        public JsonResult WxJokeDetail(int id, int typeid)
        {
            object joke = new object();

            if (typeid == 1)
            {
                CW_Joke jokeModel = new CW_JokeLogic().GetModel(id);
                var model = new
                {
                    Id = jokeModel.Id,
                    Title = jokeModel.Title,
                    Content = jokeModel.Content,
                    LikeCount = 0,
                    HateCount = 0,
                    Type = 1
                };
                joke = model;

            }
            else if (typeid == 2)
            {
                CW_JokePicture jokeModel = new CW_JokePictureLogic().GetModel(id);
                var model = new
                {
                    Id = jokeModel.Id,
                    Title = jokeModel.Title,
                    Content = jokeModel.Content,
                    LikeCount = 0,
                    HateCount = 0,
                    Type = 2
                };
                joke = model;
                
            }

            else
            {
                CW_JokeVideo jokeModel = new CW_JokeVideoLogic().GetModel(id);
                var model = new
                {
                    Id = jokeModel.Id,
                    Title = jokeModel.Title,
                    Content = jokeModel.Pic,
                    LikeCount = 0,
                    HateCount = 0,
                    Type = 3
                };
                joke = model;
            }

            if (joke != null)
            {
                var obj = new
                {
                    state = "success",
                    joke = joke
                };
                return Json(obj);
            }
            else
            {
                var obj = new
                {
                    state = "norecord"
                };
                return Json(obj);
            }

        }

        private List<object> randomList(List<object> jokes)
        {
            List<object> jokesNew = new List<object>();
            while (jokes.Count > 0)
            {
                int rdm = new Random().Next(jokes.Count);
                object obj = jokes[rdm];
                jokesNew.Add(obj);
                jokes.RemoveAt(rdm);
            }

            return jokesNew;
        }

        [HttpPost]
        public JsonResult RandomSidebar(int? type)
        {
            List<object> jokes = new List<object>();
            if (type == null)
                type = 1;
            if (type == 1)
            {
                SP_Pagination sp = new SP_Pagination
                {
                    TableName = "(select top 10 Id,Title,[Content],LikeCount,HateCount from dbo.CW_Joke where IsShow=1 order by newid())temp",
                    Fields = "*",
                    OrderField = "Id",
                    PageSize = 10,
                    PageIndex = 1,
                    SortAsc = true,
                    strWhere = ""
                };
                DataTable dt = sp.GetList().Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var model = new
                    {
                        Id = dt.Rows[i]["Id"].ToString(),
                        Title = dt.Rows[i]["Title"].ToString(),
                        Content = dt.Rows[i]["Content"].ToString(),
                        LikeCount = dt.Rows[i]["LikeCount"].ToString(),
                        HateCount = dt.Rows[i]["HateCount"].ToString(),
                        Type = 1
                    };
                    jokes.Add(model);
                }
            }
            else if (type == 2)
            {
                SP_Pagination sp2 = new SP_Pagination
                {
                    TableName = "(select top 6 Id,Title,[Content],LikeCount,HateCount from dbo.CW_JokePicture where IsShow=1 order by newid())temp",
                    Fields = "*",
                    OrderField = "Id",
                    PageSize = 10,
                    PageIndex = 1,
                    SortAsc = true,
                    strWhere = ""
                };
                DataTable dt2 = sp2.GetList().Tables[0];
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    var model = new
                    {
                        Id = dt2.Rows[i]["Id"].ToString(),
                        Title = dt2.Rows[i]["Title"].ToString(),
                        Content = dt2.Rows[i]["Content"].ToString(),
                        LikeCount = dt2.Rows[i]["LikeCount"].ToString(),
                        HateCount = dt2.Rows[i]["HateCount"].ToString(),
                        Type = 2
                    };
                    jokes.Add(model);
                }
            }
            var obj = new
            {
                state = "success",
                jokes = jokes
            };
            return Json(obj);

        }


        [HttpPost]
        public JsonResult Vote(int? jokeid, int? type, int? isLike)
        {
            if (jokeid > 0 && type > 0)
            {
                if (type == 1)
                {
                    CW_Joke joke = new CW_JokeLogic().GetModel(Convert.ToInt32(jokeid));
                    if (joke != null)
                    {
                        if (isLike == 1)
                        {
                            joke.LikeCount = joke.LikeCount == null ? 0 : joke.LikeCount;
                            joke.LikeCount = joke.LikeCount + 1;
                        }
                        else
                        {
                            joke.HateCount = joke.HateCount == null ? 0 : joke.HateCount;
                            joke.HateCount = joke.HateCount + 1;
                        }
                        new CW_JokeLogic().Update(joke);
                    }
                }
                else if (type == 2)
                {
                    CW_JokePicture joke = new CW_JokePictureLogic().GetModel(Convert.ToInt32(jokeid));
                    if (joke != null)
                    {
                        if (isLike == 1)
                        {
                            joke.LikeCount = joke.LikeCount == null ? 0 : joke.LikeCount;
                            joke.LikeCount = joke.LikeCount + 1;
                        }
                        else
                        {
                            joke.HateCount = joke.HateCount == null ? 0 : joke.HateCount;
                            joke.HateCount = joke.HateCount + 1;
                        }

                        new CW_JokePictureLogic().Update(joke);
                    }
                }
            }

            var obj = new
            {
                state = "success"
            };
            return Json(obj);

        }
    }
}
